<?
/*************************************************************
 * 新闻基础类

 * Version:				2.0
 * Author				Warmy
 * Create Date			2008-01-17
 * flag					0	普通

 						1	推荐置首页

 						2	推荐
 						4	置定
 ************************************************************/
class WNews extends Athena 
{
	/**
	 * 接收多选CHECKBOX的值

	 *
	 * @param string $key		表单的名称

	 * @return unknown			多选表单的值

	 */
	private $pulish_url	= "";
	//存放静态页路径
	protected $dump_source;
	protected $dump_path;
	protected $dump_name;
	protected $dump_list;
	protected $dump_list_name;
	protected $typeid = 0;
	
	/**
	 * *
	 *直接初始化干预条件

	 * @return unknown
	 */
	function Check_Extend_Where()
	{
		return "where typeid={$this->typeid} ";
	}
	
	/**
	 * 直接设置新增记录需要初始化而又不能修改的参数

	 *
	 */
	function Save_Add()
	{
		parent::Save_Add('`typeid`',$this->typeid);
	}
	
	
	function __construct($db,							//数据库句柄

					$Athena_Inital_Array,			//结构初始化表
					$pagesize = PAGESIZE					//分页尺寸
					)
	{
		$this->typeid = $GLOBALS['ATHENA_MODEL'];
		parent::Athena($db,$Athena_Inital_Array,$pagesize);
	}
	
	function Initialize_publish($Athena_Inital_Array)
	{
		//模板存放目录
		$this->dump_source	= "";
		//导出静态页存放路径
		$this->dump_path	= "/";
		$sql	= "select url from function where id='{$GLOBALS['Athena_Model']}'";
		$row	= $this->db->query_row($sql,MYSQL_NUM);
		if(count($row)==1)
		{
			$this->pulish_url = $row[0][0];
			$this->dump_path  = $row[0][0];
			//$arr	= pathinfo($this->pulish_url);
			//$this->dump_path = $arr['dirname'] . "/";
			//创建发布目录
			
		}
		else 
			$this->halt("功能管理设置有错误！");
		//模板名称
		$this->dump_name		= $Athena_Inital_Array["publish"]["template_name"];
		$this->dump_list_name	= $Athena_Inital_Array["publish"]["template_list_name"];
		//替换对照表

		$this->dump_list			= Array('title'=>'{$title}','content'=>'{$content}');
	}
	
	
	
	//生成静态页面

	function publish_page($id,$add = true)
	{
		if($this->dump_name == "")
			return;
		//初始化模板类
		if($this->dump_source == "")
			$wtemplate	= new WTemplate($this->db,DEBUG,true);
		else 
			$wtemplate	= new WTemplate($this->db,$this->dump_source,DEBUG,true);
		//加载模板内容
		$wtemplate->Load_Template($this->dump_name);
		//设置存放静态页地址
		$wtemplate->set_dump(dirname($this->dump_path));
		//分配数据
		//$len	= count($this->dump_list);
		$row	= $this->get_publish_data($id);
		foreach ($this->dump_list as $key=>$val)
		{
			if(isset($row[$key]))
				$wtemplate->Assign($val,$row[$key]);
			else 
				$this->halt("assign 对照表设置有错误！");
		}
		$wtemplate->Display("$id.html");
		//发布列表页面
		if($this->dump_list_name)
		{
			$this->publish_list($wtemplate,$add);
		}
	}
	
	function get_publish_data($id)
	{
		$sql	= "select * from {$this->table} where `{$this->primary}` = '$id'";
		$row	= $this->db->query_row($sql,MYSQL_ASSOC);
		if(count($row) == 1)
			return $row[0];
		else 
			return array();
	}
	
	function publish_list($wtemplate,$add)
	{
		$wtemplate->Load_Template($this->dump_list_name);
		$row	= $this->db->query_row("select count(*) from {$this->table} where typeid='{$this->typeid}'",MYSQL_NUM);
		$recordcount	= $row[0][0];//$recordcount=29;
		$pagecount		= ceil($recordcount / PAGESIZE);
		$lastpage		= $recordcount % PAGESIZE;
		if($add == false)
		{
			//由当前编辑页获取记录在分页时起始值和结束值

			$begin		= $recordcount - $this->page * PAGESIZE;
			if($pagecount == 1)
				$end	= $recordcount;
			else
				$end		= $begin + PAGESIZE;
			$pageB 		= ceil($begin / PAGESIZE);
			$pageE		= ceil($end	/ PAGESIZE);
			if(($lastpage < 10) == true)
				$pagecount--;
			$fn	= basename($this->dump_path);

			if($pageB == $pagecount && $pageB>0)
			{
				$start	= ($pagecount - 1)*PAGESIZE;
				$size	= $start + PAGESIZE + $lastpage;
				$this->publish_list_item($wtemplate,$fn,$pagecount,$start,$size,$pagecount,$lastpage);
			}
			else 
			{	
				if($pageB>1)
				{
					$start	= ($pageB-1) * PAGESIZE;
					$this->publish_list_item($wtemplate,$this->PageNavigatorFileName($fn,$pageB),$pageB,$start,PAGESIZE,$pagecount,$lastpage);
				}
				if($pageE == $pagecount)
				{
					$start	= ($pagecount - 1)*PAGESIZE;
					$size	= $start + PAGESIZE + $lastpage;
					$this->publish_list_item($wtemplate,$fn,$pagecount,$start,$size,$pagecount,$lastpage);
				}
				else 
				{
					$start	= ($pageE-1) * PAGESIZE;
					if($pagecount > 0)
						$this->PageNavigatorFileName($fn,$pageE);
					$this->publish_list_item($wtemplate,$fn,$pageE,$start,PAGESIZE,$pagecount,$lastpage);
				}
			}
		}
		else 
		{
			if(($lastpage < 10) == true)
				$pagecount--;
			
			for($i=1;$i<$pagecount;$i++)
			{
				$fn		= DOCUMENTROOT . $this->PageNavigatorFileName($this->dump_path,$i);
				if(file_exists($fn) && $lastpage != 10)
					continue;
				else 
				{
					$fn	= basename($fn);
					$start	= ($i-1) * PAGESIZE;
					$this->publish_list_item($wtemplate,$fn,$i,$start,PAGESIZE,$pagecount,$lastpage);
				}
			}
			//生成索引页面
			if($pagecount>1)
				$start	= ($pagecount-1) * PAGESIZE;
			else 
				$start	= 0;
			if($lastpage<10)
				$size = PAGESIZE+$lastpage;
			else 
				$size = $lastpage;
			$this->publish_list_item($wtemplate,basename($this->dump_path),$pagecount,$start,$size,$pagecount,$lastpage);
		}
	}
	function publish_list_item($wtemplate,$filename,$current,$start,$row_count,$pagecount,$lastpage)
	{
		$pattern = "\\{wnewslist\\}(.*)\\{/wnewslist\\}";
		$li		 = "";
		//ob_clean();
		$arr	= array();
		if(ereg($pattern,$wtemplate->template_content,$arr))
			$li = $arr[1];
		if($li == "")
			$this->halt("模板不符合规范！");
		
		$str	= "";	
		$sql	= "select id,title from `{$this->table}` where typeid='{$this->typeid}' order by flag,id limit $start," . $row_count;
		$row	= $this->db->query_row($sql,MYSQL_ASSOC);
		$pattern = "|\\{(.[^}]*)\\}|U";
		preg_match_all($pattern,$li,$arr,PREG_PATTERN_ORDER);
		while(list(,$value) = each($row))
		{
			$li_tmp	= $li;
			reset($arr[1]);
			while(list(,$field) = each($arr[1]))
			{
				if(isset($value[$field]))
					$li_tmp	= str_replace("{".$field."}",$value[$field],$li_tmp);
			}
			//print($li_tmp);
			//print("<br><br><br>");
			$str = "{$li_tmp}$str";
		}
		$wtemplate->Assign_Clear();
		$wtemplate->Assign("{wnewslist}$li{/wnewslist}",$str);
		$wtemplate->Assign("{pagenav}",$this->PageNavigator(basename($this->dump_path),$current,$pagecount,$lastpage));
		//print($this->PageNavigator(basename($this->dump_path),$i,$pagecount,$lastpage));
		//print("$filename 呵呵");exit;
		$wtemplate->Display($filename);
	}
	function PageNavigator($filename,$current,$total)
	{
		$retval	= "";
		if($current == 1)
		{
			if($total == 1)
				$retval	= "";
			elseif($total == 2)
				$retval	= "<a href=\"$filename\">上一页</a>";
			else 
				$retval	= "<a href=\"".$this->PageNavigatorFileName($filename,2)."\">上一页</a>";
			
		}
		else 
		{
			if($current<$total)
			{
				if($current == ($total - 1))
					$retval	= "$retval<a href=\"$filename\">上一页</a>";
				else 
					$retval = "$retval<a href=\"".$this->PageNavigatorFileName($filename,$current+1)."\">上一页</a>&nbsp;";
			}
			$retval	= "$retval<a href=\"".$this->PageNavigatorFileName($filename,$current-1)."\">下一页</a>&nbsp;";
			
		}
		return $retval;
	}
	function PageNavigatorFileName($filename,$page)
	{
		$pattern 	= "\\.(html|htm)$";
		$filename	= eregi_replace($pattern,"-$page.\\1",$filename);
		return $filename;
	}
	function Del_Publish_File($id)
	{
		if($this->dump_name == "")
			return;
		$len	= count($id);
		for($i=0;$i<$len;$i++)
		{
			@unlink(DOCUMENTROOT.dirname($this->dump_path)."/{$id[$i]}.html");
		}
		$row	= $this->db->query_row("select count(*) from {$this->table} where typeid='{$this->typeid}'",MYSQL_NUM);
		$recordcount	= $row[0][0];
		$pagecount		= ceil($recordcount / PAGESIZE);
		$lastpage		= $recordcount % PAGESIZE;
		$begin			= $recordcount - $this->page * PAGESIZE;
		if(($lastpage < 10) == true)
			$pagecount--;
		$pageB 		= floor($begin / PAGESIZE);
		$pageB		= ($pageB==0)?1:$pageB;
		$pagecount_old	= ceil(($recordcount + $len)/PAGESIZE);
		$fn			= basename($this->dump_path);
		for($i=$pageB;$i<$pagecount_old;$i++)
		{
			@unlink(DOCUMENTROOT.dirname($this->dump_path)."/".$this->PageNavigatorFileName($fn,$i));
		}
		@unlink(DOCUMENTROOT.dirname($this->dump_path)."/$fn");
		//初始化模板类
		if($this->dump_source == "")
			$wtemplate	= new WTemplate($this->db,DEBUG,true);
		else 
			$wtemplate	= new WTemplate($this->db,$this->dump_source,DEBUG,true);
		$wtemplate->Load_Template($this->dump_list_name);
		$wtemplate->set_dump(dirname($this->dump_path));
		
		for($i=$pageB;$i<$pagecount;$i++)
		{
			$fn	= $this->PageNavigatorFileName(basename($this->dump_path),$i);
			$start	= ($i-1) * PAGESIZE;
			$this->publish_list_item($wtemplate,$fn,$i,$start,PAGESIZE,$pagecount,$lastpage);
		}
		//生成索引页面
		$start	= ($pagecount-1) * PAGESIZE;
		if($lastpage<10)
			$size = PAGESIZE+$lastpage;
		else 
			$size = $lastpage;
		$fn	= basename($this->dump_path);
		$this->publish_list_item($wtemplate,$fn,$pagecount,$start,$size,$pagecount,$lastpage);
	}
	function __set($key,$val)
	{
		parent::__set($key,$val);
		switch($key)
		{
			case "typeid":
				$this->typeid = $val;
		}
	}
	
	function getMax_Idx()
	{
		$where = $this->getMax_Idx_ExtentdWhere($this->extend_where);
		$where = $where=='' ? "where typeid={$GLOBALS['ATHENA_MODEL']} ":"{$where} and typeid={$GLOBALS['ATHENA_MODEL']}";
		//print($where);
		$value = $this->db->query_value("select max(`{$this->idx_field_name}`) from `{$this->table}` {$where}",MYSQL_NUM);
		$value = $value==''?1:$value+1;
		return $value;
	}
} 
?>